import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CodeGen {
	private final static Logger log = LoggerFactory.getLogger(CodeGen.class);

	/**
	 * 字符串变成新文件23333
	 * */
	public static void strToDoc(String filename, String str, boolean flag) {
		FileOutputStream fostemp;
		try {
			fostemp = new FileOutputStream(filename, flag);
			PrintStream out = new PrintStream(
					new BufferedOutputStream(fostemp), false, "utf-8");
			out.print(str);
			out.close();
			fostemp.close();
			log.info("generete " + filename);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
	}

	
	
	public static void genCode(String moduleName, String modelName,
			String tplModelName) throws Exception {

		String smallTemplateName = tplModelName.substring(0, 1).toLowerCase()+ tplModelName.substring(1, tplModelName.length());
		String smallModelName = modelName.substring(0, 1).toLowerCase() + modelName.substring(1, modelName.length());
		String bigTemplateName = tplModelName.substring(0, 1).toUpperCase()+ tplModelName.substring(1, tplModelName.length());
		String bigModelName = modelName.substring(0, 1).toUpperCase() + modelName.substring(1, modelName.length());
		
		String path_tpl_java = "src/main/java/com/mindao/app/ued/";
		String path_tpl_web = "src/main/webapp/app/ued/" + tplModelName+ "/";
		String path_ouput_java = "src/main/java/com/mindao/app/" + moduleName + "/";
		String path_output_web = "src/main/webapp/app/" + moduleName + "/"+ smallModelName + "/";
		FileUtils.deleteDirectory(new File(path_output_web));
		FileUtils.forceMkdir(new File(path_output_web));
		FileUtils.forceMkdir(new File(path_ouput_java + "action"));
		FileUtils.forceMkdir(new File(path_ouput_java + "model"));
			// 先生成网页
			File filedir = new File(path_tpl_web);
			filedir.list();
			for (String tplName : filedir.list()) {
				FileInputStream fi = new FileInputStream(path_tpl_web + "/"
						+ tplName);
				byte[] b = new byte[fi.available()];
				fi.read(b);
				String tmpFirstlowCase = new String(b).replaceAll(
						smallTemplateName, smallModelName);
				String tmpbig= tmpFirstlowCase.replaceAll(
						bigTemplateName, bigModelName);				
				String result1 = tmpbig;
				String result = result1.replaceAll("manager", moduleName); // .不能作为字符串
				strToDoc(
						"src/main/webapp/app/"
								+ moduleName.toLowerCase()
								+ "/"
								+ modelName.toLowerCase()
								+ "/"
								+ tplName.replace(smallTemplateName,
										smallModelName), result, false);
			}
			// 生成java
			System.out.println("模板目录" + path_tpl_java);
			String[] java_file_array = new String[] {
					"controller/" + bigTemplateName + "Controller.java",
					"model/" + bigTemplateName + ".java" };
			for (String tplName : java_file_array) {
				System.out.println("模板java文件：" + path_tpl_java + tplName);
				FileInputStream fi = new FileInputStream(path_tpl_java
						+ tplName);
				byte[] b = new byte[fi.available()];
				fi.read(b);
				String tmpFirstlowCase = new String(b).replaceAll(
						smallTemplateName, smallModelName);
				String result1 = tmpFirstlowCase.replaceAll(
						bigTemplateName, bigModelName);
				System.out.println("第一次替换内容：" + result1);
				String result = result1.replaceAll("manager", moduleName); // .不能作为字符串
				System.out.println("第2次替换内容：" + result);

				strToDoc(
						path_ouput_java
								+ tplName.replace(bigTemplateName,
										bigModelName), result, false);
			}
			String menu_str = "<li><a href=\"" + moduleName + "_"+ smallModelName + "_menu_"+ "list.do\" target=\"mainFrame\">" + modelName+ "</a><li/>\n";
			System.out.println("代码生成完毕!\n请刷新ECLIPSE查看生成代码\n"+menu_str);
			
		
	}

	/**
	 * 参数1： 对象名，请用小写， 参数2：模板类型 : tpltb1 ajax输入类型 tpltb2 表单形式输入 tpltree1 属性输入
	 * 参数3: java路径 参数4: web路径
	 * */
	// alige-product-backlog ,alige-meeting,alige-sprint
	public static void main(String[] args) throws Exception {
		
		CodeGen.genCode("ued", "maprow", "tpltb3");
		CodeGen.genCode("ued", "mapcol", "tpltb3");

	}

}
